﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_ArticleList : System.Web.UI.PageEx
{
    private string strPageCode = "N0000B";
    private string strLoginResult = string.Empty;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            DataBinds();
        }
    }
    protected void DataBinds()
    {
        List<Model.DbClass> lstClass = new List<Model.DbClass>();
        DalEx.DbClassEx.GetClassList(0, " and cType=0", ref lstClass);
        ddlSearchClass.DataSource = lstClass;
        ddlSearchClass.DataValueField = "CId";
        ddlSearchClass.DataTextField = "CName";
        ddlSearchClass.DataSimulateTreeLevelField = "CLevel";
        ddlSearchClass.DataBind();
        ddlSearchClass.Items.Insert(0, new FineUI.ListItem("--所有类别--", string.Empty));
        strQuery = " and aLang='News'";
        DataSet ds = WebBase.Comm.SelectByPage("dbArticles", "*", strQuery, "aId", "aId", 0, 1, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();
    }
    protected void gridDataList_RowDataBound(object sender, FineUI.GridRowEventArgs e)
    {
        DataRowView drv = (DataRowView)gridList.Rows[e.RowIndex].DataItem;
        Model.DbClass dbClass = DalEx.DbClassEx.Select(Convert.ToInt32(drv["aClassId"]));
        if (dbClass != null)
        {
            Label lblClass = (Label)gridList.Rows[e.RowIndex].FindControl("lblClass");
            lblClass.Text = dbClass.CName;
        }        
    }
    protected void gridList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        DataRow mydw = drv.Row;
        int status = Convert.ToInt32(mydw["aStatus"]);
        if (status == 1)
        {
            btnShow.CommandName = "Hide";
            btnShow.Icon = FineUI.Icon.Decline;
            btnShow.ToolTip = "隐藏当前行数据";
            btnShow.ConfirmText = "是否确定隐藏当前行数据";
        }
        else
        {
            btnShow.CommandName = "Show";
            btnShow.Icon = FineUI.Icon.Accept;
            btnShow.ToolTip = "显示当前行数据";
            btnShow.ConfirmText = "是否确定显示当前行数据";
        }
}
    protected void gridList_RowCommand(object sender, FineUI.GridCommandEventArgs e)
    {
        int iSelectId = 0;
        Dictionary<int, Dictionary<int, string>> modifiedDict = gridList.GetModifiedDict();
        string strCommand = e.CommandName;
        int iResult = 0;
        string strResult = string.Empty;
        if (!string.IsNullOrEmpty(strCommand))
        {
            int iSelectIndexId = e.RowIndex;
            string strSelectId = gridList.Rows[iSelectIndexId].DataKeys[0].ToString();

            Int32.TryParse(strSelectId, out iSelectId);
            FineUI.Alert.Show(strCommand);
            switch (strCommand)
            {
                case "Show":
                    iResult = DalEx.DbArticlesEx.UpdateStatus(iSelectId, 1, ref strResult);
                    break;
                case "Hide" :
                    iResult = DalEx.DbArticlesEx.UpdateStatus(iSelectId, 0, ref strResult);
                    break;
                case "Delete":
                    iResult = DalEx.DbArticlesEx.Delete(" and aId=" + iSelectId, ref strResult);
                    break;
            }
            if (iResult > 0)
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
                DataBinds();
            }
            else
            {
                FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Warning);
            }
        } 
    }
    protected void btnSelectDelete_Click(object sender, EventArgs e)
    {
        string strResult = string.Empty;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        foreach (int iSelectId in lstSelectRowId)
        {
            DalEx.DbArticlesEx.Delete(" and aId=" + iSelectId, ref strResult);
        }
        FineUI.Alert.Show(strResult, FineUI.MessageBoxIcon.Information);
        DataBinds();
    }
    protected void ttboxKeywords_Trigger1Click(object sender, EventArgs e)
    {
        ddlSearchClass.SelectedIndex = 0;
        ttboxKeywords.Text = string.Empty;
    }
    protected void ttboxKeywords_Trigger2Click(object sender, EventArgs e)
    {       
        strQuery = string.Empty;
        string strSelectClass = ddlSearchClass.SelectedValue.Trim();
        string strKeywords = SqlHelper.String(ttboxKeywords.Text.Trim());
        if (!string.IsNullOrEmpty(strSelectClass))
            strQuery += " and aClassId = " + strSelectClass;
        if (!string.IsNullOrEmpty(strKeywords))
            strQuery += " and (aTitle like '%" + strKeywords + "%' or aNote like '%" + strKeywords + "%')";

        DataBinds();
        ddlSearchClass.SelectedValue = strSelectClass;
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        int iPageSize = 20;
        int.TryParse(ddlPageSize.SelectedValue, out iPageSize);
        gridList.PageSize = iPageSize;
        DataBinds();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        DataBinds();
    }
    protected void gridList_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {
        gridList.PageIndex = e.NewPageIndex;
        DataBinds();
    }
    protected void btnAddArticle_Click(object sender, EventArgs e)
    {
        winForm.Hidden = false;
        winForm.IFrameUrl = "ArticlesAdd.aspx";
        winForm.IFrameName = "新增文章资料";
        winForm.Title = "新增文章资料";
    }

}
